package org.benben;

import java.io.*;
import java.util.regex.*;
import java.util.*;
import java.sql.*;

public class HtmlParserTest
{
    public static void main(String[] args) throws IOException
    {
        HtmlParser htmlParser = new HtmlParser();
        String s = "   class   =   \"title\"   abc  =  \"wersdfsdewr\"";
        StringBuilder stringBuilder = new StringBuilder();
        
        String fileName = "/home/lixiao/a.html";

        //String fileName = "C:\\Users\\i007123\\Desktop\\a.html";
        String fileEncoding = "utf-8";
        FileInputStream fileInputStream = new FileInputStream(fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, fileEncoding);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        HashSet<String> urlSet = new HashSet<String>();
        String urlString = "";
        /*
        String regx = "[A-Za-z]+[ ]*=[ ]*\"[^\"]+\"";
        Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(s);
        System.out.println(matcher.matches());
        System.out.println(matcher.groupCount());
        while(matcher.find())
        {
            int start = matcher.start();
            int end = matcher.end();
            String keyValue = s.substring(start, end);
            System.out.println(s.substring(start, end));
             
            int index = keyValue.indexOf("=");
            String key = keyValue.substring(0,index).trim();
            int index1 = keyValue.indexOf("\"");
            int index2 = keyValue.lastIndexOf("\"");
            String value = keyValue.substring(index1 + 1, index2);
            System.out.println("[" + key + "," + value + "]");
        }
        */
        String line;
        while((line = bufferedReader.readLine()) != null)
            stringBuilder.append(line);
        s = stringBuilder.toString();
        
        htmlParser.setContent(s);
        //System.out.println(s);
        long t1 = System.currentTimeMillis();
        //htmlParser.parse();
        long t2 = System.currentTimeMillis();
        //System.out.println("Total costs " + (t2 - t1) + " milli seconds.");
        //String nextTag = htmlParser.getNextTe(s);
        
        //System.out.println("<" + nextTag + ">");
        //System.out.println(htmlParser.isComment(s));
        
        /*
        int index = nextTag.indexOf(" ");
        if(index > 0)
        {
            String tagName = nextTag.substring(0, index);
            String value = nextTag.substring(index + 1, nextTag.length());
            System.out.println(tagName);
            System.out.println(value);
            HashMap<String, String> res = htmlParser.getProperties(value);
            Set<String> keySet = res.keySet();
            for(String key : keySet)
                System.out.println("[" + key + "," + res.get(key) + "]");
        }
        
        bufferedReader.close();
        String urlContent = "";
        
        urlString = "http://www.taobao.com";
        urlSet.add(urlString);
        HttpRequest httpRequest = new HttpRequest(urlString);
        if(httpRequest.connect() == true)
        {
            System.out.println("Connection successful!");
            //System.out.println("Content:" + httpRequest.getContent());
            System.out.println("Content type:" + httpRequest.getCotentType());
            System.out.println("Encoding:" + httpRequest.getEncoding());
            System.out.println("Last modified:" + httpRequest.getLastModified());
            System.out.println("Response code:" + httpRequest.getResponseCode());
            System.out.println("Content length:" + httpRequest.getContentLength());
            System.out.println("URL:" + httpRequest.getURL());
            
            urlContent = httpRequest.getURLContent();
            //System.out.println("URL content:" + urlContent);
        }
        else
        {
            System.out.println("Connection failed.");
        }
        
        
        htmlParser = new HtmlParser();
        
        htmlParser.setContent(urlContent);
        htmlParser.parse();
        String [] urlList = htmlParser.getHyperLinks();
        t1 = System.currentTimeMillis();
        for(int i = 0;i < urlList.length;++i)
        {
            urlString = urlList[i];
            if(urlSet.contains(urlString) == true)
                continue;
            else
                urlSet.add(urlString);
            
            if(urlString.equals("#") == true)
                continue;
            
            if(urlString.startsWith("http://") == false && urlString.startsWith("https://") == false)
            {
                System.out.println(false);
                String urlHead = httpRequest.getURL();
                urlString = urlHead + urlString;
            }
            System.out.println(i + ":URL to crawl:" + urlString);   
            HttpRequest httpRequest2 = new HttpRequest(urlString);
            if(httpRequest2.connect() == true)
            {
                System.out.println("Connect successful");
                String urlContent2 = httpRequest2.getURLContent();
                System.out.println("Encoding:" + httpRequest2.getEncoding());
                System.out.println("Last modified:" + httpRequest2.getLastModified());
                System.out.println("Response code:" + httpRequest2.getResponseCode());
                System.out.println("Content length:" + httpRequest2.getContentLength());
                //System.out.println("URL content:[" + urlContent2 + "]");
                HtmlParser htmlParser2 = new HtmlParser();
                htmlParser2.setContent(urlContent2);
                htmlParser2.parse();
                String [] temp = htmlParser2.getHyperLinks();
                System.out.println("total url:" + temp.length);
            }
            else
                System.out.println("Connect failed");
        }
        
        t2 = System.currentTimeMillis();
        
        System.out.println("It costs " + (t2 - t1) + " milli seconds to crawl " + urlList.length + " urls.");
        
        
        */
        //System.out.println("ee");
        
        //String dbDriver = "com.sap.db.jdbc.Driver";
        String dbDriver = "com.mysql.jdbc.Driver";
        //String dbURL = "jdbc:sap://10.128.80.150:30015/?reconnect=true&currentschema=lx";
        String dbURL = "jdbc:MySQL://127.0.0.1:3306/test";
        //String dbUser = "SYSTEM";
        String dbUser = "lixiao";
        //String dbPassword = "manager";
        String dbPassword = "123456";
        
        DataBase dbConnection = new DataBase(dbDriver, dbURL, dbUser, dbPassword);
        if(dbConnection.connect() == false)
            return;
        
        System.out.println("Connection successful.");
        //dbConnection.setSchema("JZHS");
            
        String temp = "insert into `student` values(9, 'Fatty', 171.0, 150, '1983-01-01 12:00:00', 'M')";
        String [] multiSQL = new String[1000];
        for(int i = 0;i < multiSQL.length;++i)
            multiSQL[i] = "insert into `student` values(" + (int)(Math.random() * 100) + ", 'Fatty'," + Math.random() * 200 + ", 150, '1983-01-01 12:00:00', 'M')";
            
        /*
        String tableName = "student";
        String [] fields = {"id", "name", "height", "weight", "birthday", "sex"};
            
        int count = 1000;
        String [][] values = new String[count][];
        for(int i = 0;i < count;++i)
            values[i] = new String[]{"" + (int)(Math.random() * 100) + "", "Fatty", "171.0", "150", "1983-01-01 12:00:00", "M"};
        */
        t1 = Utils.getTimer();
        //boolean code = dbConnection.executeInsert(tableName, fields, values);
        boolean code = dbConnection.executeInsert(multiSQL);
        t2 = Utils.getTimer();
        System.out.println("It costs " + (t2 - t1) + " milli seconds to insert data.");
        if(code == true)
            System.out.println("Insert successful.");
        else
            System.out.println("Insert failed.");
        
        //Update
        String sql = "UPDATE student set height = height + 6, weight = weight + 5, birthday = '1987-09-03 12:00:00'";
        multiSQL = new String[1];
        for(int i = 0;i < 1;++i)
            multiSQL[i] = sql;
        t1 = Utils.getTimer();
        code = dbConnection.executeUpdate(sql);
        t2 = Utils.getTimer();
        System.out.println("It costs " + (t2 - t1) + " milli seconds to update.");
        if(code == true)
            System.out.println("Update successfull.");
        else
            System.out.println("Update failed.");
        
        String tableName = "student";
        String [] fields = null;
        //select
        sql = "select count(*) from student";
        fields = new String[]{"id", "name", "height", "weight"};
        String [] conditions = new String[]{"height > 200", "id > 30"};
        ResultSet result = dbConnection.executeQuery(tableName, fields, conditions);
        
        try
        {
            System.out.println(result.toString());
            while(result.next())
            {
                //System.out.println("id:" + result.getInt(1) + ",name:" + result.getString(2) + 
                //        ",height:" + result.getFloat(3) + ",weight:" + result.getDouble(4) +  
                //        ",birthday:" + result.getTimestamp(5) + ",sex:" + result.getString(6));
                //System.out.println("count:" + result.getInt(1));
                System.out.println("id:" + result.getInt(1) + ",name:" + result.getString(2) +
                        ",height:" + result.getFloat(3) + ",weight:" + result.getDouble(4));
            }
        }catch(SQLException ex)
        {
            ex.printStackTrace();
        }
             
        Log log = new Log("/home/lixiao/", "a.log");
        log.setLogPath();
        log.output("aaaaererw");
    }
}

